/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * @description: WebUtils
 * @author: zzh
 * @date: 2021/9/15 16:55
 */
public class WebUtils {

  /**
   * 判断是否是multipart/form-data请求
   *
   * @param request 请求信息
   * @return 是否是multipart/form-data请求
   */
  public static boolean isMultipartContent(HttpServletRequest request) {
    // 获取Content-Type
    String contentType = request.getContentType();
    return (contentType != null) && (contentType.toLowerCase().startsWith("multipart/"));
  }

  /**
   * 向上下文添加数据
   *
   * @param key
   * @param value
   */
  public static void addAttribute(ServletContext context, String key, Object value) {
    context.setAttribute(key, value);
  }

  /**
   * 向请求添加数据
   *
   * @param request
   * @param key
   * @param value
   */
  public static void addAttribute(HttpServletRequest request, String key, Object value) {
    request.setAttribute(key, value);
  }

  /**
   * 向上下文移除数据
   *
   * @param context
   * @param key
   */
  public static void removeAttribute(ServletContext context, String key) {
    context.removeAttribute(key);
  }

  /**
   * 从请求对象移除数据
   *
   * @param request
   * @param key
   */
  public static void removeAttribute(HttpServletRequest request, String key) {
    request.removeAttribute(key);
  }
}
